package control;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import entities.tools.FilledForm;
import entities.tools.Form;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.ejb.FilledFormBeanInterface;
import util.ejb.FormBeanInterface;
import util.ejb.FormSignerBeanInterface;

/**
 *
 * @author Marek
 */
@WebServlet(urlPatterns = {"/raport.jsp"},loadOnStartup=1)
@ServletSecurity(
@HttpConstraint(transportGuarantee = TransportGuarantee.CONFIDENTIAL,rolesAllowed = {"admin"}))
public class RaportServlet  extends AbstractServlet {
    @PersistenceContext(name = "test", unitName = "JEE-study-warPU")
    private EntityManager em;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            HttpSession session = request.getSession(true);

            Form form = (Form) session.getAttribute("form");
            if (null == form) {
                form = formBean.getExistingForm(new Long(1));
                session.setAttribute("form", form);
            }

            /*
             select p from Product p 
             inner join p.descriptor d 
             where p.code = d.code;
             */
            //This don't work as I imagine it to work. It is printing to many records. I need to learn how joins work in the standard sql question, and then I need to learn how works joins in the jpql question
            //List<FilledForm> filledForms = em.createQuery("select f from FilledForm f inner join f.formSigner fs inner join f.answers an where an.filledForm=f and fs=f.formSigner and f.form=:form",FilledForm.class).setParameter("form",form).getResultList();
            //TODO: I should create new method returning all filled forms for a one form entity
            List<FilledForm> filledForms = filledFormBean.getFilledForms();
            session.setAttribute("filledForms", filledForms);
            request.getRequestDispatcher("WEB-INF/raportTmpl.jsp").include(request, response);
        }
    }

}
